package com.moyq5.permit.center.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.PageData;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult.Status;
import com.moyq5.permit.center.bean.embed.MerchRoleId;
import com.moyq5.permit.center.bean.entity.Merch;
import com.moyq5.permit.center.bean.entity.MerchRole;
import com.moyq5.permit.center.bean.search.MerchRole4Search;
import com.moyq5.permit.center.feign.client.MerchRoleFeign;
import com.moyq5.permit.center.web.jackson.filter.MerchRoleFilter;

/**
 * 商户角色
 * @author Moyq5
 * @date 2019年7月25日
 */
@RestController
@RequestMapping("/merchRole")
public class MerchRoleController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(MerchRoleController.class);
	
	@Autowired
	private MerchRoleFeign merchRoleFeign;
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param merchRole4s
	 * @param pd
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page/role", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String pageWithRole(MerchRole4Search merchRole4s, PageData pd) throws JsonProcessingException {
		log.debug("查询商户角色信息>>>pageWithRole(MerchRole4Search, PageData)");
		if (null != getLoginMerchId()) {
			if (null == merchRole4s.getMerch()) {
				merchRole4s.setMerch(new Merch());
			}
			merchRole4s.getMerch().setMerchId(getLoginMerchId());;
		}
		return toJson(merchRoleFeign.pageWithRole(new Body<MerchRole4Search, PageData>(merchRole4s, pd)), MerchRoleFilter.getWithRole());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param merchRole4s
	 * @param pd
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page/merchAndRole", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String pageWithMerchAndRole(MerchRole4Search merchRole4s, PageData pd) throws JsonProcessingException {
		log.debug("查询商户角色信息>>>pageWithMerchAndRole(MerchRole4Search, PageData)");
		if (null != getLoginMerchId()) {
			if (null == merchRole4s.getMerch()) {
				merchRole4s.setMerch(new Merch());
			}
			merchRole4s.getMerch().setMerchId(getLoginMerchId());;
		}
		return toJson(merchRoleFeign.pageWithMerchAndRole(new Body<MerchRole4Search, PageData>(merchRole4s, pd)), MerchRoleFilter.getWithMerchAndRole());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param merchRole
	 * @return
	 */
	@PutMapping("/add")
	@ResponseBody
	public ObjectResult<MerchRoleId> add(@RequestBody MerchRole merchRole) {
		log.debug("添加商户角色信息>>>add(MerchRole)");
		if (null != getLoginMerchId()) {
			merchRole.getId().setMerchId(getLoginMerchId());
		}
		ObjectResult<MerchRole> objRst = merchRoleFeign.one(merchRole.getId());
		if (objRst.getStatus() == Status.OK && null != objRst.getData()) {
			ObjectResult<MerchRoleId> idRst = new ObjectResult<MerchRoleId>();
			idRst.setOk();
			idRst.setData(merchRole.getId());
			return idRst;
		}
		return merchRoleFeign.create(merchRole);
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param merchRole
	 * @return
	 */
	@PatchMapping("/edit")
	@ResponseBody
	public StatusResult edit(@RequestBody @Validated MerchRole merchRole) {
		log.debug("更新商户角色信息>>>edit(MerchRole)");
		if (null != getLoginMerchId()) {
			merchRole.getId().setMerchId(getLoginMerchId());
		}
		return merchRoleFeign.update(merchRole);
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param merchRoleId
	 * @return
	 */
	@DeleteMapping("/del")
	@ResponseBody
	public StatusResult del(@RequestBody MerchRoleId merchRoleId) {
		log.debug("删除商户角色信息>>>del(MerchRoleId)");
		if (null != getLoginMerchId()) {
			merchRoleId.setMerchId(getLoginMerchId());
		}
		return merchRoleFeign.remove(merchRoleId);
	}
	
}
